<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品标题">
              <a-input placeholder="请输入商品标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品货号">
              <a-input placeholder="请输入商品货号" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">

      <a-radio-group :default-value="null" v-model="queryParam.status" @change="searchQuery" button-style="solid">
        <a-radio-button :value="null">
          全部
        </a-radio-button>
        <a-radio-button :value="1">
          上架中
        </a-radio-button>
        <a-radio-button :value="2">
          仓库中
        </a-radio-button>
        <a-radio-button :value="0">
          下架中
        </a-radio-button>
      </a-radio-group>
      <a-divider type="vertical"/>
      <a-button @click="handlePageAdd" type="primary" icon="plus">新增</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item v-if="queryParam.status==0||queryParam.status==2" key="1" @click="batchStatusChange(1,'上架')">
            <a-icon type="edit"/>
            上架
          </a-menu-item>
          <a-menu-item v-if="queryParam.status==1" key="2" @click="batchStatusChange(0,'下架')">
            <a-icon type="edit"/>
            下架
          </a-menu-item>
          <a-menu-item key="7" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
               :dataSource="dataSource" :pagination="ipagination"
               :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               class="j-table-force-nowrap"
               @change="handleTableChange">

        <template slot="statusSlot" slot-scope="text">
          <a-tag color="orange" v-if="text==2">
            仓库中
          </a-tag>
          <a-tag color="green" v-if="text==1">
            上架中
          </a-tag>
          <a-tag color="red" v-if="text==0">
            下架中
          </a-tag>
        </template>

        <template slot="labelSlot" slot-scope="text,record">
          <div v-for="(item,index) in record.labelNamesList" :key="index" style="padding-bottom:2px;">
            <a-tag color="blue">
              {{ item }}
            </a-tag>
          </div>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="numToFixed2Slot" slot-scope="text">
          {{ text|numToFixed2 }}
        </template>
        <template slot="sequence" slot-scope="text">
          <div>
            {{ text }}
          </div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" @click="openUrlView(text)" height="55px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handlePageEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handlePageDetail(record)">详情</a>
              </a-menu-item>
              <!--
              <a-menu-item>
                <a @click="natureEdit(record)">服务描述</a>
              </a-menu-item> -->

              <a-menu-item v-if="record.status==0||record.status==2">
                <a @click="statusChange(1,record.id,'上架')">上架</a>
              </a-menu-item>
              <a-menu-item v-if="record.status==1">
                <a @click="statusChange(0,record.id,'下架')"> 下架</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.lbId)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <gms-good-nature-ref ref="modalNatures" :selectionType="'checkbox'" @ok="modalNatureOk"></gms-good-nature-ref>

  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/MyJeecgListMixin'
import {getAction, postAction, putAction} from '@/api/manage'
import JDate from '@/components/jeecg/JDate.vue'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import GmsGoodNatureRef from '@/views/gospel/mallBase/good/modules/GmsGoodNatureRef'


export default {
  name: 'LbmsGoodList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate,
    JDictSelectTag,
    GmsGoodNatureRef
  },
  data() {
    return {
      select_id: null,
      description: '商品管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        // {
        //   title: '商品编号',
        //   align: "center",
        //   dataIndex: 'id'
        // },
        {
          title: '图片',
          align: "center",
          dataIndex: 'imgUrl',
          scopedSlots: {customRender: "imgSlot"}
        },
        {
          title: '名称',
          align: "left",
          dataIndex: 'title'
        },
        {
          title: '货号',
          align: "center",
          dataIndex: 'code'
        },
        {
          title: '礼包等级',
          align: "center",
          dataIndex: 'level',
          customRender: function (text) {
            return text == 1 ? '初级' : text == 2 ? '中级' : '高级'
          }
        },
        {
          title: '现价',
          align: "center",
          dataIndex: 'price',
          sorter: true,
          scopedSlots: {customRender: "numToFixed2Slot"}
        },
        {
          title: '库存',
          align: "center",
          sorter: true,
          dataIndex: 'stock'
        },
        {
          title: '总销量',
          align: "center",
          sorter: true,
          dataIndex: 'dealNum'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
          sorter: true,
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '商品状态',
          align: "center",
          dataIndex: 'status',
          sorter: true,
          scopedSlots: {customRender: "statusSlot"}
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/mallPlus/lbmsGood/list",
        delete: "/mallPlus/lbmsGood/delete",
        editNature: "/mallBase/gmsGood/editNature",
        deleteBatch: "/mallBase/gmsGood/deleteBatch",
        editStatusBatch: "/mallBase/gmsGood/editStatusBatch",
        editStatus: "/mallBase/gmsGood/editStatus"
      },
      dictOptions: {},
    }
  },
  created() {
    this.isorter = {
      column: 'sequence',
      order: 'asc',
    }
    this.loadData()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    handlePageAdd() {
      this.$router.push({path: '/hand/setting/LbmsGoodEdit', query: {disabled: false}})
    },
    handlePageEdit(record) {
      this.$router.push({path: '/hand/setting/LbmsGoodEdit', query: {id: record.id, disabled: false}})
    },
    handlePageDetail(record) {
      this.$router.push({path: '/hand/setting/LbmsGoodEdit', query: {id: record.id, disabled: true}})
    },

    natureEdit(record) {
      let self = this
      self.select_id = record.id
      self.$refs.modalNatures.visible = true
      self.$refs.modalNatures.checkedKeys = []
      self.$refs.modalNatures.checkedRows = record.natureInfoObj
      for (let i = 0; i < record.natureInfoObj.length; i++) {
        self.$refs.modalNatures.checkedKeys.push(record.natureInfoObj[i].id)
      }
      self.$refs.modalNatures.open()
    },
    modalNatureOk(rows, keys) {
      var that = this
      if (that.select_id) {
        putAction(that.url.editNature, {id: that.select_id, natureInfo: JSON.stringify(rows)}).then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.loadData()
            that.onClearSelected()
          } else {
            that.$message.warning(res.message)
          }
        }).finally(() => {
          that.loading = false
        })
      }

    },
    initDictConfig() {
    },
    isActMsg() {
      this.$message.warning('处于预售活动中，不能上架！')
    },
    //status: 0=下架,1=上架(通过审核),2=提交审核 ,3=违规下架
    batchStatusChange: function (status, str) {
      if (!this.url.editStatusBatch) {
        this.$message.error("请设置url.editStatusBatch!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ""
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ","
        }
        var that = this
        this.$confirm({
          title: "确认批量" + str,
          content: "是否批量" + str + "选中数据?",
          onOk: function () {
            that.loading = true
            getAction(that.url.editStatusBatch, {ids: ids, status: status}).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },
    statusChange: function (status, id, str) {
      if (!this.url.editStatus) {
        this.$message.error("请设置url.editStatus!")
        return
      }
      var that = this
      this.$confirm({
        title: "确认" + str,
        content: "是否" + str + "选中数据?",
        onOk: function () {
          that.loading = true
          getAction(that.url.editStatus, {id: id, status: status}).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.loading = false
          })
        }
      })

    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>